package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.Log;
import com.atguigu.jxc.service.CustomerReturnListGoodsService;
import com.atguigu.jxc.service.GoodsService;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.service.SaleListGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private LogService logService;
    //获取编码
    @Override
    public ServiceVO getCode() {
        // 获取当前商品最大编码
        String code = goodsDao.getMaxCode();
        // 在现有编码上加1
        Integer intCode = Integer.parseInt(code) + 1;
        // 将编码重新格式化为4位数字符串形式
        String unitCode = intCode.toString();
        for(int i = 4;i > intCode.toString().length();i--){
            unitCode = "0"+unitCode;
        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, unitCode);
    }
    //当前库存分页查询
    @Override
    public Map<String, Object> listInventory(Integer page, Integer rows, String codeOrName, Integer goodsTypeId) {
        //创建一个map封装前端所需要的信息
        Map<String,Object> map = new HashMap<>();
        //当前页
        page  = page == 0 ? 1 : page;
        //每页从第几条数据开始
        int offSet = (page - 1) * rows;
        //查新库存
        List<Goods> goodsList =  goodsDao.getGoodsInventoryList(offSet, rows, codeOrName, goodsTypeId);
        //封装数据
        map.put("rows",goodsList);
        map.put("total",goodsDao.getGoodsInventoryCount(codeOrName, goodsTypeId));
        //日志打印
        logService.save(new Log(Log.SELECT_ACTION, "分页查询商品库存信息"));
        //返回
        return map;
    }
    //分页查询商品信息
    @Override
    public Map<String, Object> listGoods(Integer page, Integer rows, String goodsName, Integer goodsTypeId) {
        //创建一个map封装前端所需要的信息
        Map<String,Object> map = new HashMap<>();
        //当前页
        page = page == 0 ? 1 : page;
        //每页从第几条数据开始
        int offSet = (page - 1) * rows;
        //查询商品信息
        List<Goods> goodsList = goodsDao.getGoodsList(offSet, rows, goodsName, goodsTypeId);
        //封装数据
        map.put("rows",goodsList);
        map.put("total",goodsDao.getGoodsCount(goodsName,goodsTypeId));
        //返回
        return map;
    }
    //商品添加或修改
    @Override
    public ServiceVO saveGoods(Goods goods) {
        if(goods.getGoodsId() != null){//判断有无商品id
            //有就是更改
            goodsDao.updateGoods(goods);
        }else{
            //没有就走保存
            goodsDao.saveGoods(goods);
        }
        //返回
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }
    //商品删除
    @Override
    public ServiceVO deleteGoods(Integer goodsId) {
        //查询出将要删除的商品
        Goods good = goodsDao.selectOneGood(goodsId);
        if(good.getState().equals(0)){//如果状态为0，可以删除
            goodsDao.deleteGoods(goodsId);
            return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
        }else {
            //状态是1或2，不能删除
            return new ServiceVO(ErrorCode.STORED_ERROR_CODE,ErrorCode.STORED_ERROR_MESS);
        }


    }
    //无库存商品列表展示
    @Override
    public Map<String,Object> getNoInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        //创建map封装前端需要的数据
        Map<String,Object> map = new HashMap<>();
        //当前页
        page = page == 0 ? 1 : page;
        //每页数据从哪儿开始
        int offSet = (page - 1) * rows;
        //查询无库存商品
        List<Goods> goodsNoInventoryQuantityList = goodsDao.getNoInventoryQuantityList(offSet,rows,nameOrCode);
        //封装数据
        map.put("rows",goodsNoInventoryQuantityList);
        map.put("total",goodsDao.getNoInventoryQuantityCount(nameOrCode));
        //返回
        return map;
    }
    //有库存商品列表展示
    @Override
    public Map<String, Object> getHasInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        //创建map封装前端需要的数据
        Map<String,Object> map = new HashMap<>();
        //当前页
        page = page == 0 ? 1 : page;
        //每页数据从哪儿开始
        int offSet = (page - 1) * rows;
        //查询无库存商品
        List<Goods> goodsHasInventoryQuantityList = goodsDao.getHasInventoryQuantityList(offSet,rows,nameOrCode);
        //封装数据
        map.put("rows",goodsHasInventoryQuantityList);
        map.put("total",goodsDao.getHasInventoryQuantityCount(nameOrCode));
        //返回
        return map;
    }
    //添加库存、修改数量或成本价
    @Override
    public ServiceVO saveStock(Integer goodsId, Integer inventoryQuantity, double purchasingPrice) {
        //添加或修改
        goodsDao.updateStock(goodsId,inventoryQuantity,purchasingPrice);

        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }
    //查询所有 当前库存量 小于 库存下限的商品信息
    @Override
    public Map<String, Object> selectListAlarm() {
        Map<String, Object> map = new HashMap<>();
        List<Goods> goodsList = goodsDao.selectListAlarm();
        map.put("rows",goodsList);
        return map;
    }


}
